package business

import (
	"context"
	"errors"
	"group-buy/group_buy/internal/models"

	"group-buy/group_buy/internal/svc"
	"group-buy/group_buy/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetBusinessByUserIdLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetBusinessByUserIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetBusinessByUserIdLogic {
	return &GetBusinessByUserIdLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetBusinessByUserIdLogic) GetBusinessByUserId(req *types.GetBusinessByUserIdReq) (resp *types.Business, err error) {
	m := &models.Business{UserId: req.UserId}
	get, err := l.svcCtx.Engine.Get(m)
	if err != nil {
		return nil, err
	}
	if !get {
		return nil, errors.New("门店不存在")
	}

	return &types.Business{
		Id:            m.Id,
		Name:          m.Name,
		ContactsName:  m.ContactsName,
		ContactsPhone: m.ContactsPhone,
		Address:       m.Address,
		WorkTime:      m.WorkTime,
		Preferential:  m.Preferential,
		Img:           m.Img,
		Sort:          m.Sort,
		UserId:        m.UserId,
		DealerId:      m.DealerId,
	}, nil
}
